<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;
use app\api\error\Position as PositionError;

/**
 * 职位接口逻辑
 */
class Position extends ApiBase
{
	/**
	 * 标记面试
	 */
	public function interviewStat($data){
		$res=db('interview')->where(array('id'=>$data['id'],'user_id'=>$data['user_id']))->update(array('stat'=>$data['stat'],'end_time'=>time()));
		if(!empty($res)){
			return $this->apiReturn();
		}else{
			return PositionError::$paramError;
		}
	}
	/**
	 * 我发布的职位邀请
	 */
	public function interviewList($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$where=" user_id=".$data['user_id'];
		if(isset($data['type'])){
			switch($data['type']){
				case 1:
					//$time=strtotime(date('Y-m-d'));
					//$where.=" and  unix_timestamp(`day`) <".$time;
					$where.=" and ms_time <".time();
					$sort=" day desc ";
					break;
				case 2:
					$where.=" and day =date_format(now(), '%Y-%m-%d') and ms_time >".time();
					$sort=" ms_time asc ";
					break;
				case 3:
					$time=strtotime(date('Y-m-d'));
					$where.=" and unix_timestamp(`day`) >".$time;
					$sort=" day asc,ms_time asc ";
					break;
				default:
					$sort=" end_time desc";
			}
		}else{
			$sort=" end_time desc";	
		}
		
		if(!empty($data['stat'])){
			$where.=" and stat=".$data['stat'];
		}
		if(isset($data['is_reward'])){
			$where.=" and is_reward=".$data['is_reward'];
		}
		$list=db('interview')->where($where)->limit($page,$data['list_rows'])->order($sort)->select();
		foreach($list as $k=>$v){
			$list[$k]['name']=db('resume')->where('user_id',$v['getid'])->value('name');
			$list[$k]['uphone']=db('resume')->where('user_id',$v['getid'])->value('phone');
		}
		return $list;
	}
	/**
	 * 我的面试邀请
	 */
	public function interviewLists($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$time=strtotime(date('Y-m-d'))-24*60*60;
		if(!empty($data['stat'])){
			$where=" getid=".$data['user_id']." and stat=".$data['stat'];
		}else{
			$where=" unix_timestamp(`day`) >".$time." and getid=".$data['user_id'];
		}
		$list=db('interview')->where($where)->limit($page,$data['list_rows'])->order('day asc,his asc')->select();
		foreach($list as $k=>$v){
			$workinfo=db('work')->field('title,min,max,ismy,user_id,is_salary')->where('id',$v['work_id'])->find();
			$workinfo['company']=db('company')->where('user_id',$workinfo['user_id'])->value('company');
			$list[$k]['workinfo']=$workinfo;
		}
		return $list;
	}
	/**
	 * 我发布的职位
	 */
	public function userWorkList($data){
		$where=" stat=0 and status=1 and user_id=".$data['user_id'];
		$list=db('work')->field('id,title')->where($where)->order('id desc')->select();
		return $list;
	}
	/**
	 * 面试邀请
	 */
	public function interviewAdd($data){
		$validate_result = $this->validateInterview->scene('interview')->check($data);
		if (!$validate_result) {
			return [API_CODE_NAME => 1020002, API_MSG_NAME => $this->validateInterview->getError()];
		}
		$data['is_reward']=db('work')->where('id',$data['work_id'])->value('is_reward');
		$data['reward']=db('work')->where('id',$data['work_id'])->value('reward');
		$data['create_time']=time();
		$data['ms_time']=strtotime($data['ms_time']);
		$res=db('interview')->insertGetId($data);
		if($res != false){
			jpush(strval($data['getid']),5,'有用人单位对您的简历感兴趣啦！');
			$email=db('resume')->where('user_id',$data['getid'])->value('email');
			if(!empty($email)){
				emailModel($email,$data);
			}
		}
		return $this->apiReturn();
	}
	/**
	 * 标记各种简历数量(个人)
	 */
	public function resumeNums($data){
		$time=strtotime(date('Y-m-d'))-24*60*60;
		$arr=array(
			'td'=>db('deliver_resume')->where(array('user_id'=>$data['user_id']))->count(),
			'dms'=>db('interview')->where("unix_timestamp(`day`) >".$time." and getid=".$data['user_id'])->count(),
			'yly'=>db('interview')->where(array('getid'=>$data['user_id'],'stat'=>1))->count(),
			'sc'=>db('shoucang')->where(array('user_id'=>$data['user_id'],'type'=>1))->count()
		);
		return $arr;
	}
	/**
	 * 标记各种简历数量(企业)
	 */
	public function resumeNum($data){
		$time=strtotime(date('Y-m-d'))-24*60*60;
		$times=strtotime(date('Y-m-d'));
		$ids=db('work')->where(array('user_id'=>$data['user_id'],'stat'=>0))->column('id');
		if(!empty($ids)){
			$str=implode(',',$ids);
			$where=" work_id IN(".$str.") ";
		}else{
			$where=" 1=2 ";
		}
		$arr=array(
			'is'=>db('interview')->where("user_id=".$data['user_id']." and stat=0 and ms_time <".time())->count(),
			'td'=>db('deliver_resume')->where($where)->count(),
			'dms'=>db('interview')->where("unix_timestamp(`day`) >".$time." and user_id=".$data['user_id'])->count(),
			'yly'=>db('interview')->where(array('user_id'=>$data['user_id'],'stat'=>1))->count(),
			'sc'=>db('shoucang')->where(array('user_id'=>$data['user_id'],'type'=>1))->count(),
			'mr'=>db('interview')->where("unix_timestamp(`day`) >".$times." and user_id=".$data['user_id'])->count(),
		);
		return $arr;
	}
    /**
     * 职位分类
     */
    public function getPositonCat($pid=0){
		$position_list=db('position_cat')->where('pid',$pid)->order('sort ASC')->select();
		if(!empty($position_list)){
			foreach( $position_list as $k=>$val){
				$position_list[$k]['catlist']=$this->getPositonCat($val['id']);			
			}		
		}
		return $position_list;		
	}
	/**
     * 获取职位、学历、经验列表
     */
    public function getPositonCats(){
		$position_list=$this->getPositonCat();
		$experience_list=db('experience')->select();
		$salary_list=db('salary')->select();
		$education_list=db('education')->select();	
		return compact('position_list','experience_list','salary_list','education_list');		
	}
	/**
     * 关闭职位职位
     */
	public function closeWork($data){
		db('work')->startTrans();
		$res=db('work')->where(array('id'=>$data['id'],'user_id'=>$data['user_id']))->update(array('stat'=>$data['stat'],'update_time'=>time()));
		if($data['stat'] == 0){
			$re=logAdd($data['user_id'],$data['id'],0);
		}else{
			$re=false;
		}
		if($res && !$re){
			db('work')->commit();
			return $this->apiReturn();
		}else{
			db('work')->rollback();
			return ['code' => 1020001, 'msg' => $re];
		}		
		
	}
	/**
     * 发布职位
     */
	public function releaseWork($data){
		$validate_result = $this->validateWork->scene('rWork')->check($data);      
        if (!$validate_result) {           
            return PositionError::$paramErrorFail;
        } 
		if(empty($data['id'])){
			$data['status']=1;
			$data['create_time']=time();
			$data['update_time']=time();
			db('work')->startTrans();
			$res=db('work')->insertGetId($data);
			$re=logAdd($data['user_id'],$res,0);
			if($res && !$re){
				db('work')->commit();				
			}else{
				db('work')->rollback();
				return ['code' => 1020001, 'msg' => $re];
			}
		}else{
			$res=db('work')->where('id',$data['id'])->update($data);
		}		
		return array('id'=>$res);	
	}
	/**
     * 我发布的职位
     */
	public function myWork($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$where=" status=1 and user_id=".$data['user_id'];
		if(isset($data['stat'])){
			$data['stat']=!empty($data['stat'])?$data['stat']:0;
			$where.=" and stat=".$data['stat'];
		}
		if(isset($data['is_reward'])){
			$where.=" and is_reward=".$data['is_reward'];
		}
		if(!empty($data['keywords'])){
			$keywords=$data['keywords'];
			$where.=" and title like '%$keywords%'";
		}
		$list=db('work')->where($where)->limit($page,$data['list_rows'])->order('id desc')->select();	
		foreach($list as $k=>$v){
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['experience']=db('experience')->where('experience_id',$v['experience_id'])->value('name');
			$list[$k]['position']=db('position_cat')->where('id',$v['position_id'])->value('catname');
			$list[$k]['company']=db('company')->where('user_id',$v['user_id'])->value('company');
		}
		return $list;	
	}
	/**
     * 职位详情
     */
	public function workInfo($data){
		$info=db('work')->where('id',$data['work_id'])->find();
		$info['cont']=htmlspecialchars_decode($info['cont']);
		$info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
		$info['experience']=db('experience')->where('experience_id',$info['experience_id'])->value('name');
		$info['position']=db('position_cat')->where('id',$info['position_id'])->value('catname');
		$cinfo=db('company')->where('user_id',$info['user_id'])->find();
		$cinfo['typeName']=db('company_type')->where('id',$cinfo['type'])->value('name');
		$cinfo['numName']=db('company_num')->where('id',$cinfo['num'])->value('name');
		$cinfo['headimg']=db('user')->where('id',$info['user_id'])->value('headimg');
		$info['company']=$cinfo;
		if(!empty($data['user_id'])){
			$time=db('deliver_resume')->where(array('user_id'=>$data['user_id'],'work_id'=>$data['work_id']))->value('create_time');
			if(empty($time) || $time-time()>30*24*60*60){
				$info['is']=0;
			}else{
				$info['is']=1;
				$info['td_time']=$time;
			}
			$info['issc']=db('shoucang')->where(array('user_id'=>$data['user_id'],'work_id'=>$data['work_id']))->value('id');
		}
		return $info;	
	}
	/**
	 * 搜索职位
	 */
	public function workList($data){
		$list_rows=!empty($data['list_rows'])?$data['list_rows']:10;
		$page=!empty($data['page'])?$data['page']:1;
		$start=($page-1)*$list_rows;
		$where=" a.status=1 and a.stat=0 ";
		$list=db('work')->alias('a')->field('a.*,b.company,b.num,b.type')->join("hr_company b","a.user_id=b.user_id",'LEFT')->where($where)->limit($start,$list_rows)->order(" a.update_time desc,a.create_time desc")->select();
		foreach($list as $k=>$v){
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['experience']=db('experience')->where('experience_id',$v['experience_id'])->value('name');
			$list[$k]['position']=db('position_cat')->where('id',$v['position_id'])->value('catname');
			$list[$k]['typeName']=db('company_type')->where('id',$v['type'])->value('name');
			$list[$k]['numName']=db('company_num')->where('id',$v['num'])->value('name');
			$list[$k]['headimg']=db('user')->where('id',$v['user_id'])->value('headimg');
		}
		return $list;
	}
	/**
     * 简历匹配职位
     */
	public function matchWork($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$info=db('resume')->field('city,position_id,education_id,ismy,min,max')->where('resume_id',$data['resume_id'])->find();
		if(empty($info)){
			return PositionError::$paramError;
		}
		$city=!empty($info['city'])?$info['city']:'成都';
		//$where=" status=1 and stat=0 and  position_id= ".$info['position_id']." and education_id >= ".$info['education_id']." and address like '%$city%' ";
		$where=" status=1 and stat=0 and position_id= ".$info['position_id'];
		if($info['ismy'] !=1 && !empty($info['max']) && !empty($info['min']) ){
			//$where.=" and (ismy=1 or (ismy =0 and min <= ".$info['max']." and max >= ".$info['min'].")) ";
		}	

		$list=db('work')->where($where)->limit($page,$data['list_rows'])->order(" update_time desc,create_time desc")->select();
		foreach($list as $k=>$v){
			$list[$k]['istd']=db("deliver_resume")->where(array('resume_id'=>$data['resume_id'],'work_id'=>$v['id']))->value('id');
			$info=db('company')->field('company,type,num')->where('user_id',$v['user_id'])->find();
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['experience']=db('experience')->where('experience_id',$v['experience_id'])->value('name');
			$list[$k]['position']=db('position_cat')->where('id',$v['position_id'])->value('catname');
			//pc新增
			$list[$k]['company']=$info['company'];
			$list[$k]['typeName']=db('company_type')->where('id',$info['type'])->value('name');
			$list[$k]['numName']=db('company_num')->where('id',$info['num'])->value('name');
			$list[$k]['headimg']=db('user')->where('id',$v['user_id'])->value('headimg');
		}
		return $list;	
	}
	/**
	 * 搜索职位(单表查询，备份)
	 */
	public function searchWorks($data){
		$list_rows=!empty($data['list_rows'])?$data['list_rows']:10;
		$page=!empty($data['page'])?$data['page']:1;
		$start=($page-1)*$list_rows;
		$where=" status=1 and stat=0 ";
		if(!empty($data['keywords'])){
			$keywords=trim($data['keywords']);
			$where.=" and title like '%$keywords%'";
		}
		if(isset($data['isjz'])){
			$data['isjz']=!empty($data['isjz'])?$data['isjz']:0;
			$where.=" and isjz =".$data['isjz'];
		}
		if(!empty($data['position_id'])){
			$arr=db('position_cat')->where("pid=".$data['position_id'])->column('id');
			if(!empty($arr)){
				$s=implode(',',$arr);
				$where.=" and position_id IN(".$s.")";

			}else{
				$where.=" and position_id =".$data['position_id'];
			}
		}
		if(!empty($data['education_id'])){
			$where.=" and education_id >= ".$data['education_id'];
		}
		if(!empty($data['is_urgent'])){
			$where.=" and is_urgent = ".$data['is_urgent'];
		}
		if(!empty($data['is_reward'])){
			$where.=" and is_reward = ".$data['is_reward'];
		}
		if(!empty($data['codes'])){
			$codes=$data['codes'];
			$arr=db('region')->where("code = '".$codes."' or city_code = '".$codes."' or 	province_code= '".$codes."'")->column('code');
			$str=implode(',',$arr);
			$where.=" and codes IN(".$str.")";
			//$str=substr($data['codes'],0,-2);
			//$where.=" and codes like '%$str%'";
		}
		$list=db('work')->where($where)->limit($start,$list_rows)->order(" update_time desc,create_time desc")->select();
		foreach($list as $k=>$v){
			$list[$k]['company']=db('company')->where('user_id',$v['user_id'])->value('company');
			$list[$k]['companyName']=db('company')->where('user_id',$v['user_id'])->value('title');
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['experience']=db('experience')->where('experience_id',$v['experience_id'])->value('name');
			$list[$k]['position']=db('position_cat')->where('id',$v['position_id'])->value('catname');
		}
		return $list;
	}
	/**
	 * 搜索职位
	 */
	public function searchWork($data){
		$list_rows=!empty($data['list_rows'])?$data['list_rows']:10;
		$page=!empty($data['page'])?$data['page']:1;
		$start=($page-1)*$list_rows;
		$where=" a.status=1 and a.stat=0 ";
		if(!empty($data['keywords'])){
			$keywords=trim($data['keywords']);
			$where.=" and (a.title like '%$keywords%' or b.company like '%$keywords%')";
		}
		if(isset($data['isjz'])){
			$data['isjz']=!empty($data['isjz'])?$data['isjz']:0;
			$where.=" and a.isjz =".$data['isjz'];
		}
		if(!empty($data['position_id'])){
			$arr=db('position_cat')->where("pid=".$data['position_id'])->column('id');
			if(!empty($arr)){
				$s=implode(',',$arr);
				$where.=" and a.position_id IN(".$s.")";

			}else{
				$where.=" and a.position_id =".$data['position_id'];
			}
		}
		if(!empty($data['education_id'])){
			$where.=" and a.education_id >= ".$data['education_id'];
		}
		//经验筛选
		if(!empty($data['experience'])){
			$e=$data['experience'];
			$where.=" and a.experience_id = ".$e;
		}
		if(!empty($data['is_urgent'])){
			$where.=" and a.is_urgent = ".$data['is_urgent'];
		}
		if(!empty($data['is_reward'])){
			$where.=" and a.is_reward = ".$data['is_reward'];
		}
		//地区筛选
		if(!empty($data['codes'])){
			$code=$data['codes'];
			$where.=" and a.codes IN(".$code.")";
		}
		if(!empty($data['code'])){
			$codes=$data['code'];
			$arr=db('region')->where("code = '".$codes."' or city_code = '".$codes."' or 	province_code= '".$codes."'")->column('code');
			$str=implode(',',$arr);
			$where.=" and a.codes IN(".$str.")";
		}
		//薪资判断
		if(!empty($data['max'])){
			$min=$data['min'];
			$max=$data['max'];
			$where.=" and ((a.min >= ".$min." and a.min <= ".$max.") or (a.max >= ".$min." and a.max <= ".$max.") or a.ismy=1) ";
		}elseif(!empty($data['min']) && empty($data['max'])){
			$min=$data['min'];
			$where.=" and (a.max >= ".$min." or a.ismy=1)";
		}
		//判断发布时间
		if(!empty($data['day'])){
			$start_time=time()-24*60*60;
			$where.=" and a.update_time > ".intval($start_time)." ";
		}
		if(!empty($data['sort'])){
			if(!empty($data['asc'])){
				$order=" a.".$data['sort']." desc,a.update_time desc";
			}else{
				$order=" a.".$data['sort']." asc,a.update_time desc";
			}
		}else{
			$order=" a.update_time desc,a.create_time desc";
		}
		$list=db('work')->alias('a')->field('a.*,b.company')->join("hr_company b","a.user_id=b.user_id",'LEFT')->where($where)->limit($start,$list_rows)->order($order)->select();
		foreach($list as $k=>$v){
			$list[$k]['companyName']=db('company')->where('user_id',$v['user_id'])->value('title');
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['experience']=db('experience')->where('experience_id',$v['experience_id'])->value('name');
			$list[$k]['position']=db('position_cat')->where('id',$v['position_id'])->value('catname');
			$list[$k]['headimg']=db('user')->where('id',$v['user_id'])->value('headimg');
			if(!empty($data['user_id'])){
				$time=db('deliver_resume')->where(array('user_id'=>$data['user_id'],'work_id'=>$v['id']))->value('create_time');
				if(empty($time) || $time-time()>30*24*60*60){
					$list[$k]['is']=0;
				}else{
					$list[$k]['is']=1;
				}
			}
		}
		return $list;
	}
	/**
     * 搜索职位
     */
	public function searchWorkss($data){
		$list_rows=!empty($data['list_rows'])?$data['list_rows']:10;
		$page=!empty($data['page'])?$data['page']:1;
		$start=($page-1)*$list_rows;
		$where=" a.status=1 and a.stat=0 ";
		if(!empty($data['keywords'])){
			$keywords=trim($data['keywords']);
			$where.=" and (a.title like '%$keywords%' or b.company like '%$keywords%')";
		}
		if(isset($data['isjz'])){
			$data['isjz']=!empty($data['isjz'])?$data['isjz']:0;
			$where.=" and a.isjz =".$data['isjz'];
		}
		if(!empty($data['position_id'])){
			$arr=db('position_cat')->where("pid=".$data['position_id'])->column('id');
			if(!empty($arr)){
				$s=implode(',',$arr);
				$where.=" and a.position_id IN(".$s.")";

			}else{
				$where.=" and a.position_id =".$data['position_id'];
			}
		}
		if(!empty($data['education_id'])){
			$where.=" and a.education_id >= ".$data['education_id'];
		}
		if(!empty($data['is_urgent'])){
			$where.=" and a.is_urgent = ".$data['is_urgent'];
		}
		if(!empty($data['is_reward'])){
			$where.=" and a.is_reward = ".$data['is_reward'];
		}
		if(!empty($data['codes'])){
			$codes=$data['codes'];
			$arr=db('region')->where("code = '".$codes."' or city_code = '".$codes."' or 	province_code= '".$codes."'")->column('code');
			$str=implode(',',$arr);
			$where.=" and a.codes IN(".$str.")";
			//$str=substr($data['codes'],0,-2);
			//$where.=" and codes like '%$str%'";
		}
		if(!empty($data['sort'])){
			if(!empty($data['asc'])){
				$order=" a.".$data['sort']." desc,a.update_time desc";
			}else{
				$order=" a.".$data['sort']." asc,a.update_time desc";
			}
		}else{
			$order=" a.update_time desc,a.create_time desc";
		}
		$list=db('work')->alias('a')->field('a.*,b.company')->join("hr_company b","a.user_id=b.user_id",'LEFT')->where($where)->limit($start,$list_rows)->order($order)->select();
		foreach($list as $k=>$v){
			$list[$k]['companyName']=db('company')->where('user_id',$v['user_id'])->value('title');
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['experience']=db('experience')->where('experience_id',$v['experience_id'])->value('name');
			$list[$k]['position']=db('position_cat')->where('id',$v['position_id'])->value('catname');
			$list[$k]['headimg']=db('user')->where('id',$v['user_id'])->value('headimg');
		}
		return $list;
	}
	/**
     * 发布简历
     */
	public function resumeAdd($data){
		if(empty($data['resume_id'])){
			$validate_result = $this->validateResume->scene('resume')->check($data);      
			if (!$validate_result) {           
				return [API_CODE_NAME => 1020002, API_MSG_NAME => $this->validateResume->getError()];
			} 
			$data['create_time']=time();
			$data['update_time']=time();
			$data['status']=1;
			$data['gzjy']=$this->countage($data['joinTime']);
			$data['age']=$this->countage($data['birthday']);
			$res=db('resume')->insertGetId($data);
			db('user')->where('id',$data['user_id'])->update(array('name'=>$data['name']));
			if($res != false){
				$user_id=$data['user_id'];
				$over=db('user')->where('id',$user_id)->value('over');
				if($over != 1){
					db('user')->where(array('id'=>$user_id))->update(array('over'=>1));
					$arr = parse_config_array('resumeMoney');
					if($arr['is'] == 1){
						moneyyj_log($arr['money'],$user_id,9,$user_id);
					}
				}
			}
			return array('resume_id'=>$res);
		}else{
			$validate_result = $this->validateResume->scene('resume')->check($data);      
			if (!$validate_result) {           
				return [API_CODE_NAME => 1020002, API_MSG_NAME => $this->validateResume->getError()];
			}
			//$data['update_time']=time();
			$data['gzjy']=$this->countage($data['joinTime']);
			$data['age']=$this->countage($data['birthday']);
			db('user')->where('id',$data['user_id'])->update(array('name'=>$data['name']));
			$res=db('resume')->where('resume_id',$data['resume_id'])->update($data);
			return $this->apiReturn();
		}
			
	}
	/**
     * 求职意向
     */
	public function resumeQzyx($data){
		$validate_result = $this->validateResume->scene('resumeqz')->check($data);      
		if (!$validate_result) {           
			return [API_CODE_NAME => 1020002, API_MSG_NAME => $this->validateResume->getError()];
		}
		$user_id=db('resume')->where('resume_id',$data['resume_id'])->value('user_id');
		$over=db('user')->where('id',$user_id)->value('over');
		if($over != 1){
			db('user')->where(array('id'=>$user_id))->update(array('over'=>1));
			$arr = parse_config_array('resumeMoney');
			if($arr['is'] == 1){
				moneyyj_log($arr['money'],$user_id,9,$user_id);
			}
		}
		$data['update_time']=time();
		$res=db('resume')->where(array('resume_id'=>$data['resume_id']))->update($data);
		if($res == false){
			return PositionError::$paramError;
		}
		return $this->apiReturn();
	}
	/**
     * 个人简介
     */
	public function resumeEdit($data){
		$res=db('resume')->where(array('resume_id'=>$data['resume_id'],'user_id'=>$data['user_id']))->update(array('cont'=>$data['cont'],'skill'=>$data['skill'],'update_time'=>time()));
		if($res == false){
			return PositionError::$paramError;
		}
		return $this->apiReturn();
	}
	/**
     * 添加/修改工作经历
     */
	public function resumeExperience($data){
		if(empty($data['id'])){
			$data['create_time']=time();
			$res=db('resume_experience')->insertGetId($data);
		}else{
			$res=db('resume_experience')->where('id',$data['id'])->update($data);
		}
		return array('id'=>$res);	
	}
	/**
     * 添加/修改教育经历
     */
	public function resumeEducation($data){
		if(empty($data['id'])){
			$data['create_time']=time();
			$res=db('resume_education')->insertGetId($data);
		}else{
			$res=db('resume_education')->where('id',$data['id'])->update($data);
		}
		return array('id'=>$res);	
	}
	/**
     * 删除工作经历
     */
	public function experienceDel($data){
		$res=db('resume_experience')->where(array('id'=>$data['id'],'resume_id'=>$data['resume_id']))->delete();
		if ($res != false) {
			return $this->apiReturn();	
		}else{
			return PositionError::$paramError;
		}
	}
	/**
     * 删除教育经历
     */
	public function educationDel($data){
		$res=db('resume_education')->where(array('id'=>$data['id'],'resume_id'=>$data['resume_id']))->delete();
		if ($res != false) {
			return $this->apiReturn();	
		}else{
			return PositionError::$paramError;
		}
	}
	/**
	 * 简历刷新
	 */
	public function updateResume($data){
		$time=db('resume')->where('user_id',$data['user_id'])->value('update_time');
		if(!empty($time)){
			if(date('ymd') == date('ymd',$time)){
				return PositionError::$timeOutFail;
			}
		}
		//$data['gzjy']=$this->countage($data['joinTime']);
		//$data['age']=$this->countage($data['birthday']);
		$res=db('resume')->where('user_id',$data['user_id'])->update(array('update_time'=>time()));
		if ($res != false) {
			return $this->apiReturn();
		}else{
			return PositionError::$paramError;
		}
	}
	/**
	 * 简历刷新
	 */
	public function updateWork($data){
		$time=db('work')->where(array('user_id'=>$data['user_id'],'id'=>$data['work_id']))->value('update_time');
		if(!empty($time)){
			if(date('ymd') == date('ymd',$time)){
				return PositionError::$timeOutFail;
			}
		}
		$res=db('work')->where(array('user_id'=>$data['user_id'],'id'=>$data['work_id']))->update(array('update_time'=>time()));
		if ($res != false) {
			return $this->apiReturn();
		}else{
			return PositionError::$paramError;
		}
	}
	/**
     * 简历详情
     */
	public function resumeInfo($data){
		$info=db('resume')->where(array('resume_id'=>$data['resume_id']))->find();

		if(empty($info)){
			return PositionError::$paramError;
		}
		if(!empty($info['position_id'])){
			$info['positionName']=db('position_cat')->where('id',$info['position_id'])->value('catname');
		}	
		if(!empty($info['education_id'])){
			$info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
		}
		$info['headimg']=db('user')->where('id',$info['user_id'])->value('headimg');
		$info['word']=db('user')->where('id',$info['user_id'])->value('word');
		$info['age']=$this->countage($info['birthday']);
		$info['gzjy']=$this->countage($info['joinTime']);
		$info['experienceList']=db('resume_experience')->where('resume_id',$info['resume_id'])->order('start_time desc')->select();
		$info['educationList']=db('resume_education')->where('resume_id',$info['resume_id'])->order('start_time desc')->select();
		if(!empty($data['user_id'])){
			$info['issc']=db('shoucang')->where(array('user_id'=>$data['user_id'],'resume_id'=>$data['resume_id']))->value('type');
			$info['istd']=db('deliver_resume')->where(array('getid'=>$data['user_id'],'resume_id'=>$data['resume_id']))->value('work_id');
			$info['isgt']=db('chat')->where(array('user_id'=>$data['user_id'],'resume_id'=>$data['resume_id']))->value('id');
		}
		return $info;	
	}
	/**
	 * @param $birthday 出生年月日（1992-01）
	 * @return string 年龄
	 */
	private function countage($birthday){
		$year=date('Y');
		$month=date('m');
		if(substr($month,0,1)==0){
			$month=substr($month,1);
		}
		$day=date('d');
		if(substr($day,0,1)==0){
			$day=substr($day,1);
		}
		$arr=explode('-',$birthday);

		$age=$year-$arr[0];
		if($month < intval($arr[1])){
			$age=$age-1;

		}
		//elseif($month==$arr[1]&&$day<$arr[2]){
		//	$age=$age-1;

		//}

		return $age;

	}
	/**
     * 简历投递
     */
	public function deliverResume($data){
		if($a=$this->checkWork($data['work_id'])){
			return [API_CODE_NAME => 1020002, API_MSG_NAME => $a];
		}		
		$validate_result = $this->validateDeliverResume->scene('resume')->check($data);      
		if (!$validate_result) {           
			return [API_CODE_NAME => 1020002, API_MSG_NAME => $this->validateDeliverResume->getError()];
		} 
		$create_time=db('deliver_resume')->where(array('resume_id'=>$data['resume_id'],'work_id'=>$data['work_id']))->value('create_time');
		if(!empty($create_time) ){
			if( time()-$create_time > 60*60*24*30){
				db("deliver_resume")->where(array('resume_id'=>$data['resume_id'],'work_id'=>$data['work_id']))->update(array('create_time'=>time()));
			}else{
				return PositionError::$confirmFail;
			}			
		}else{
			$is_reward=db('work')->where('id',$data['work_id'])->value('is_reward');
			if(!empty($is_reward)){
				$data['share_id']=$data['user_id'];
			}
			$data['create_time']=time();
			$data['is_reward']=$is_reward;
			$res=db('deliver_resume')->insertGetId($data);
		}
		$email=db('user')->where('id',$data['getid'])->value('email');
		if(!empty($email)){
			$txt = sprintf("有求职者向您发布的职位投递了简历！");
			send_email($email, '诸葛聘', $txt);
		}
		//推送
		jpush($data['getid'],6,'有求职者向您发布的职位投递了简历！');
		return $this->apiReturn();	
	}
	/**
     * 简历投递记录
     */
	public function deliverList($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$where="user_id=".$data['user_id'];
		if(!empty($data['is'])){
			$where.=" and `is`=1";
		}
		$list=db('deliver_resume')->where($where)->limit($page,$data['list_rows'])->order("create_time desc")->select();
		foreach($list as $k=>$v){
			$info=db('work')->where('id',$v['work_id'])->find();
			$info['company']=db('company')->where('user_id',$info['user_id'])->value('company');
			$info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
			$info['experience']=db('experience')->where('experience_id',$info['experience_id'])->value('name');
			$info['position']=db('position_cat')->where('id',$info['position_id'])->value('catname');
			$list[$k]['workinfo']=$info;
		}
		return $list;	
	}
	/**
	 * 职位匹配简历
	 */
	public function matchResume($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$where=" status=1 and isyc=0 and position_id != 0";
		$arr=db('work')->where(array('stat'=>0,'user_id'=>$data['user_id'],'status'=>1))->column('position_id');
		$str=implode(',',$arr);
		if(!empty($arr)){
			$where.=" and position_id IN(".$str.") ";
		}
		if(!empty($data['codes'])){
			$where.=" and codes=".$data['codes'];
		}
		$list=db('resume')->field("resume_id,sex,user_id,name,ismy,position_id,min,max,education_id,joinTime,position")->where($where)->limit($page,$data['list_rows'])->order("update_time desc,create_time desc")->select();
		foreach($list as $k=>$v){
			$list[$k]['headimg']=db('user')->where('id',$v['user_id'])->value('headimg');
			$list[$k]['positionName']=db('position_cat')->where('id',$v['position_id'])->value('catname');
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['gzjy']=$this->countage($v['joinTime']);
		}
		return $list;
	}
	/**
     * 简历搜索
     */
	public function resumeSearch($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$where=" status=1 and isyc=0 and city != ''";
		if(!empty($data['keywords'])){
			$keywords=$data['keywords'];
			$where.=" and position like '%$keywords%' ";
		}
		if(!empty($data['position_id'])){
			$arr=db('position_cat')->where("pid=".$data['position_id'])->column('id');
			if(!empty($arr)){
				$s=implode(',',$arr);
				$where.=" and position_id IN(".$s.")";
			}else{
				$where.=" and position_id =".$data['position_id'];
			}
		}
		if(!empty($data['education_id'])){
			$where.=" and education_id >= ".$data['education_id'];	
		}
		if(!empty($data['experience_id']) && $data['experience_id']!=1){
			$einfo=db('experience')->where('experience_id',$data['experience_id'])->find();
			$min=date('Y-m',time()-365*24*3600*($einfo['max']+1));
			$max=date('Y-m',time()-365*24*3600*$einfo['min']);
			$where.=" and joinTime > '".$min."' and joinTime <= '".$max."' ";
			//$where.=" and gzjy >= ".$einfo['min']." and gzjy <= ".$einfo['max'];
		}
		//var_dump($where);exit;
		//地区筛选
		if(!empty($data['codes'])){
			$code=$data['codes'];
			$where.=" and codes IN(".$code.")";
		}
		if(!empty($data['code'])){
			$codes=$data['code'];
			$arr=db('region')->where("code = '".$codes."' or city_code = '".$codes."' or 	province_code= '".$codes."'")->column('code');
			$str=implode(',',$arr);
			$where.=" and codes IN(".$str.")";
		}
		//薪资判断
		if(!empty($data['max'])){
			$min=$data['min'];
			$max=$data['max'];
			$where.=" and ((min >= ".$min." and min <= ".$max.") or (max >= ".$min." and max <= ".$max.") or ismy=1) ";
		}elseif(!empty($data['min']) && empty($data['max'])){
			$min=$data['min'];
			$where.=" and (max >= ".$min." or ismy=1)";
		}
		//判断发布时间
		if(!empty($data['day'])){
			$start_time=time()-24*60*60;
			$where.=" and update_time > ".intval($start_time)." ";
		}
		if(isset($data['isjz'])){
			$where.=" and isjz = ".$data['isjz'];
		}

		//if(!empty($data['codes'])){
		//	$where.=" and (codes=".$data['codes']." or codes=0)";
		//}
		if(!empty($data['sex'])){
			$where.=" and sex = ".$data['sex'];	
		}
		$list=db('resume')->field("resume_id,sex,user_id,name,ismy,position_id,min,max,education_id,joinTime,position,cont")->where($where)->limit($page,$data['list_rows'])->order("update_time desc")->select();
		foreach($list as $k=>$v){
			$list[$k]['headimg']=db('user')->where('id',$v['user_id'])->value('headimg');
			$list[$k]['positionName']=db('position_cat')->where('id',$v['position_id'])->value('catname');
			$list[$k]['education']=db('education')->where('education_id',$v['education_id'])->value('name');
			$list[$k]['gzjy']=$this->countage($v['joinTime']);
		}
		return $list;	
	}
	/**
     * 上传图片
     */
    public function uploadImg($data){
        $image=$data['image'];
		$imageName = date("ymdHis",time())."_".rand(1111,9999).'.png';
		if (strstr($image,",")){
			$image = explode(',',$image);
			$image = $image[1];
		}		
		if(!empty($data['is_company'])){
			$paths="/upload/extend/company/".date("Ymd",time());
		}else{
			$paths="/upload/extend/headimg/".date("Ymd",time());
		}
		$path = $_SERVER['DOCUMENT_ROOT'].$paths;
		if (!is_dir($path)){ //判断目录是否存在 不存在就创建
			mkdir($path,0777,true);
		}
		$imageSrc= $path."/". $imageName;  //图片名字
		$pathss= $paths."/". $imageName;
		$r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
		if ($r) {
			$arr=array('headimg'=>$pathss);
			if(!empty($data['user_id'])){
				$url=db('user')->where('id',$data['user_id'])->value('headimg');
				@unlink($_SERVER['DOCUMENT_ROOT'].$url);
				db('user')->where('id',$data['user_id'])->update($arr);
			}		
			return $arr;
		}else{
			return PositionError::$paramError;
		}
    }
	/**
     * 公司认证
     */
	public function companyAdd($data){
		if(empty($data['company_id'])){
			$validate_result = $this->validateCompany->scene('company')->check($data);      
			if (!$validate_result) {           
				return [API_CODE_NAME => 1020002, API_MSG_NAME => $this->validateCompany->getError()];
			}
			$is=db('company')->where('user_id',$data['user_id'])->value('company_id');
			if(!empty($is)){
				return [API_CODE_NAME => 1020002, API_MSG_NAME => '您已提交认证申请，请不要重复提交'];
			}
			$data['create_time']=time();
			$data['stat']=0;
			$res=db('company')->insertGetId($data);
			return array('company_id'=>$res);
		}else{
			$validate_result = $this->validateCompany->scene('company')->check($data);      
			if (!$validate_result) {           
				return [API_CODE_NAME => 1020002, API_MSG_NAME => $this->validateCompany->getError()];
			}
			$stat=db('company')->where('company_id',$data['company_id'])->value('stat');
			if($stat == 1){
				PositionError::$paramError;
			}
			$data['create_time']=time();
			$res=db('company')->where('company_id',$data['company_id'])->update($data);
			return $this->apiReturn();
		}			
	}
	/**
     * 企业性质、企业规模选择标准
     */
    public function getCompanyType(){
		$company_type=db('company_type')->select();
		$company_num=db('company_num')->select();	
		return compact('company_type','company_num');		
	}
	/**
     * 编辑公司信息
     */
	public function editCompany($data){
		if(!empty($data['resumeNum'])){
			unset($data['resumeNum']);
		}
		if(!empty($data['workNum'])){
			unset($data['workNum']);
		}
		if(!empty($data['stat'])){
			unset($data['stat']);
		}
		$res=db('company')->where(array('stat'=>1,'user_id'=>$data['user_id']))->update($data);
		return $this->apiReturn();
	}
	/**
	 * 收藏职位
	 */
	public function scWork($data){
		if(!empty($data['work_id'])){
			if($a=$this->checkWork($data['work_id'])){
				return [API_CODE_NAME => 1020002, API_MSG_NAME => $a];
			}
			$data['getid']=db('work')->where('id',$data['work_id'])->value('user_id');
			$is=db('shoucang')->where(array('user_id'=>$data['user_id'],'work_id'=>$data['work_id']))->value('id');
			if(!empty($is)){
				db('shoucang')->where(array('user_id'=>$data['user_id'],'work_id'=>$data['work_id']))->update(array('create_time'=>time()));
			}else{
				$data['create_time']=time();
				db('shoucang')->insertGetId($data);
			}
		}else{
			unset($data['access_token']);
			unset($data['user_token']);
			$is=db('shoucang')->where($data)->value('id');
			if(!empty($is) ){
				return PositionError::$confirmFail;
			}else{
				$iss=db('shoucang')->where(array('user_id'=>$data['user_id'],'resume_id'=>$data['resume_id']))->value('id');
				$data['create_time']=time();
				$data['getid']=db('resume')->where('resume_id',$data['resume_id'])->value('user_id');
				if($data['type'] == 3){
					$istd=db('deliver_resume')->where("is_reward=1 and resume_id=".$data['resume_id']." and getid=".$data['user_id'])->value('id');
					if(!empty($istd)){
						$data['is_reward']=1;
					}
				}else{
					$data['is_reward']=0;
				}
				if(!empty($iss)){
					$res=db('shoucang')->where(array('user_id'=>$data['user_id'],'resume_id'=>$data['resume_id']))->update($data);
				}else{
					$res=db('shoucang')->insertGetId($data);
				}
				if($res){
					//推送
					jpush(strval($data['getid']),5,'有用人单位对您的简历感兴趣啦！');
				}
			}
		}
		return $this->apiReturn();
	}
	/**
     * 收藏职位
     */
	public function scWorks($data){
		if(!empty($data['work_id'])){
			if($a=$this->checkWork($data['work_id'])){
				return [API_CODE_NAME => 1020002, API_MSG_NAME => $a];
			}
		}		
		unset($data['access_token']);
		unset($data['user_token']);
		$is=db('shoucang')->where($data)->value('id');
		if(!empty($is) ){		
			return PositionError::$confirmFail;
		}else{
			if(!empty($data['resume_id'])){
				$data['getid']=db('resume')->where('resume_id',$data['resume_id'])->value('user_id');
				if($data['type'] == 3){
					$istd=db('deliver_resume')->where("is_reward=1 and resume_id=".$data['resume_id']." and getid=".$data['user_id'])->value('id');
					if(!empty($istd)){
						$data['is_reward']=1;
					}
				}else{
					$data['is_reward']=0;
				}
			}else{
				$data['getid']=db('work')->where('id',$data['work_id'])->value('user_id');
			}

			$type=$data['type'];
			unset($data['type']);
			$iss=db('shoucang')->where($data)->value('id');
			if(!empty($iss)){
				$res=db('shoucang')->where($data)->update(array('create_time'=>time(),'type'=>$type,'getid'=>$data['getid']));
			}else{
				$data['type']=$type;
				$data['create_time']=time();
				$res=db('shoucang')->insertGetId($data);
			}
			if(!empty($data['resume_id']) && $res){
				$uid=db('resume')->where('resume_id',$data['resume_id'])->value('user_id');
				//推送
				jpush(strval($uid),5,'有用人单位对您的简历感兴趣啦！');
			}
		}

		return $this->apiReturn();	
	}
	/**
     * 收藏职位列表
     */
	public function scList($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		if(!empty($data['type'])){
			$where=array('user_id'=>$data['user_id'],'type'=>$data['type']);
		}else{
			$where=array('user_id'=>$data['user_id'],'type'=>1);
		}
		$list=db('shoucang')->where($where)->limit($page,$data['list_rows'])->order("create_time desc")->select();	
		foreach($list as $k=>$v){
			if(!empty($v['work_id'])){
				$info=db('work')->where('id',$v['work_id'])->find();
				$info['company']=db('company')->where('user_id',$info['user_id'])->value('company');
				$info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
				$info['experience']=db('experience')->where('experience_id',$info['experience_id'])->value('name');
				$info['position']=db('position_cat')->where('id',$info['position_id'])->value('catname');			
			}
			if(!empty($v['resume_id'])){
				$info=db('resume')->field('position_id,name,education_id,joinTime,ismy,min,max,user_id,position,birthday,sex,cont')->where('resume_id',$v['resume_id'])->find();
				$userinfo=db('user')->field('headimg,tjid')->where('id',$info['user_id'])->find();
				$info['headimg']=$userinfo['headimg'];
				$info['tjname']=db('user')->where('id',$userinfo['tjid'])->value('name');
				$info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
				$info['positionName']=db('position_cat')->where('id',$info['position_id'])->value('catname');
				$info['gzjy']=$this->countage($info['joinTime']);
				$info['age']=$this->countage($info['birthday']);
			}
			$list[$k]['info']=$info;
		}
		return $list;	
	}
	/**
     * 取消收藏职位
     */
	public function delSc($data){ 
		unset($data['access_token']);
		unset($data['user_token']);
		$data['type']=!empty($data['type'])?$data['type']:1;
		$res=db('shoucang')->where($data)->delete();
		if($res !== false){
			return $this->apiReturn();				
		}else{
			return PositionError::$paramError;
		}			
	}
	/**
     * 隐私设置
     */
	public function ysConfig($data){ 
		$res=db('resume')->where(array('user_id'=>$data['user_id']))->update(array('isyc'=>$data['isyc']));
		if($res !== false){
			return $this->apiReturn();				
		}else{
			return PositionError::$paramError;
		}			
	}
	/**
     * 标记投递简历
     */
	public function tagResume($data){
		$res=db('deliver_resume')->where(array('resume_id'=>$data['resume_id'],'getid'=>$data['user_id'],'work_id'=>$data['work_id']))->delete();
		db('shoucang')->where(array('resume_id'=>$data['resume_id'],'user_id'=>$data['user_id']))->delete();
		if($res == false){
			return PositionError::$paramError;
		}else{
			return $this->apiReturn();
		}		
	}
	/**
     * 删除简历
     */
	public function delResume($data){
		db('resume')->startTrans();
		db('resume_education')->where(array('resume_id'=>$data['resume_id']))->delete();
		db('resume_experience')->where(array('resume_id'=>$data['resume_id']))->delete();
		$res=db('resume')->where(array('resume_id'=>$data['resume_id'],'user_id'=>$data['user_id']))->delete();		
		if($res != false){
			db('resume')->commit();
			return $this->apiReturn();		
		}else{
			db('resume')->rollback();
			return PositionError::$paramError;
		}
		
	}
	/**
     * 收到的简历列表
     */
	public function receivedResume($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$ids=db('work')->where(array('user_id'=>$data['user_id'],'stat'=>0))->column('id');
		if(!empty($ids)){
			$str=implode(',',$ids);
			$where=" work_id IN(".$str.") ";
			if(!empty($data['work_id'])){
				$where.=' and work_id='.$data['work_id'];
			}
		}else{
			$where=" 1=2 ";
		}
		$list=db('deliver_resume')->where($where)->limit($page,$data['list_rows'])->order("create_time desc")->select();	
		foreach($list as $k=>$v){
			$info=db('resume')->field('position_id,name,education_id,joinTime,birthday,ismy,min,max,user_id,sex,cont')->where('user_id',$v['user_id'])->find();
			$info['headimg']=db('user')->where('id',$info['user_id'])->value('headimg');
			if(!empty($info['education_id'])){
				$info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
			}
			if(!empty($info['position_id'])){
				$info['position']=db('position_cat')->where('id',$info['position_id'])->value('catname');
			}			
			$info['gzjy']=$this->countage($info['joinTime']);
			$info['age']=$this->countage($info['birthday']);
			$list[$k]['info']=$info;
			$list[$k]['work']=db('work')->where('id',$v['work_id'])->value('title');
			$tjid=db('user')->where('id',$info['user_id'])->value('tjid');
			$list[$k]['tjid']=$tjid;
			$list[$k]['tjname']=db('user')->where('id',$tjid)->value('name');
		}
		return $list;	
	}
	/**
     * 职位检查
     */
	private function checkWork($id){
		$info=db('work')->field('status,stat')->where('id',$id)->find();
		if(empty($info)){
			return '不存在该职位';
		}
		if($info['status'] == 0){
			return '该职位招聘已关闭';
		}
		if($info['stat'] == 1){
			return '该职位已停止招聘';
		}
		return false;
	}
	/**
	 * 我的悬赏
	 */
	public function rewardWork($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$where=" is_reward=1 and share_id = ".$data['user_id'];
		$list=db('deliver_resume')->field("*,count(*) as sum")->where($where)->group('work_id')->limit($page,$data['list_rows'])->order("create_time desc")->select();
		foreach($list as $k=>$v){
			unset($lists);
			$str=" and work_id=".$v['work_id'];
			$lists=db('deliver_resume')->field('user_id,create_time,resume_id,getid')->where($where.$str)->select();
			foreach($lists as $key=>$val){
				$info=db('user')->field('headimg,name,phone')->where('id',$val['user_id'])->find();
				$lists[$key]['headimg']=$info['headimg'];
				$lists[$key]['sex']=db('resume')->where('user_id',$val['user_id'])->value('sex');
				$lists[$key]['name']=$info['name'];
				$lists[$key]['phone']=$info['phone'];
				$is=db('interview')->where(array('work_id'=>$v['work_id'],'getid'=>$val['user_id']))->value('id');
				if(!empty($is)){
					$lists[$key]['istd']=db('interview')->where(array('work_id'=>$v['work_id'],'getid'=>$val['user_id']))->order('stat desc')->value('stat');;
				}else{
					$lists[$key]['istd']=2;	
				}
			}
			$list[$k]['list']=$lists;
			$list[$k]['work']=db('work')->where('id',$v['work_id'])->value('title');
			$list[$k]['reward']=db('work')->where('id',$v['work_id'])->value('reward');
			$list[$k]['company']=db('company')->where('user_id',$v['getid'])->value('company');
		}
		return $list;
	}
	/**
	 * 已录用
	 */
	public function rewardList($data){
		$where=" is_reward=1 and share_id = ".$data['user_id'];
		$list=db('deliver_resume')->where($where)->order("end_time desc")->select();
		$i=0;
		$arr=[];
		foreach($list as $k=>$v){
			$is=db('interview')->where(array('work_id'=>$v['work_id'],'getid'=>$v['user_id']))->value('id');
			if(!empty($is)){
				$v['work']=db('work')->where('id',$v['work_id'])->value('title');
				$v['reward_type']=db('work')->where('id',$v['work_id'])->value('reward_type');
				$v['reward']=db('work')->where('id',$v['work_id'])->value('reward');
				$v['company']=db('company')->where("user_id",$v['getid'])->value('company');
				$info=db('user')->field('headimg,name,phone')->where('id',$v['user_id'])->find();
				$v['headimg']=$info['headimg'];
				$v['name']=$info['name'];
				$v['phone']=$info['phone'];
				$v['sex']=db('resume')->where('user_id',$v['user_id'])->value('sex');
				$v['stat']=db('interview')->where(array('work_id'=>$v['work_id'],'getid'=>$v['user_id']))->order('stat desc')->value('stat');
				$arr[$i]=$v;
				$i++;
			}
		}
		return $arr;
	}
	/**
	 * 我的悬赏
	 */
	public function userRewardList($data){
		$info=db('user')->field('id,name,headimg,phone')->where('id',$data['user_id'])->find();
		$where=" is_reward=1 and share_id=".$data['user_id'];
		$list=db('deliver_resume')->where($where)->order("create_time desc")->select();
		if(!empty($list)){
			$info['tdnum']=count($list);
			foreach($list as $k=>$v){
				$infos=db('resume')->field('sex,name,position,education_id,ismy,max,min,gzjy')->where('user_id',$v['user_id'])->find();
				$list[$k]['headimg']=db('user')->where('id',$v['user_id'])->value('headimg');
				$list[$k]['sex']=$infos['sex'];
				$list[$k]['name']=$infos['name'];
				$list[$k]['position']=$infos['position'];
				$list[$k]['max']=$infos['max'];
				$list[$k]['min']=$infos['min'];
				$list[$k]['gzjy']=$infos['gzjy'];
				$list[$k]['ismy']=$infos['ismy'];
				$list[$k]['education']=db('education')->where('education_id',$infos['education_id'])->value('name');
				$list[$k]['work']=db('work')->where('id',$v['work_id'])->value('title');
				$list[$k]['company']=db('company')->where('user_id',$v['getid'])->value('company');
			}
			$info['tdlist']=$list;
			$larr=array();
			foreach($list as $k=>$v){
				if(!empty($v['istd'])){
					$larr[]=$v;
				}
			}
			$info['lynum']=count($larr);
			$info['lylist']=$larr;
		}
		return $info;
	}
	/**
	 * 标记录用
	 */
	public function enrollUser($data){
		$res=db('deliver_resume')->where(array('getid'=>$data['user_id'],'id'=>$data['id']))->update(array('end_time'=>time(),'istd'=>1));
		if($res != false){
			$user_id=db('deliver_resume')->where(array('getid'=>$data['user_id'],'id'=>$data['id']))->value('user_id');
			jpush(strval($user_id),5,'有用人单位对您的简历感兴趣啦！');
			return $this->apiReturn();
		}else{
			return PositionError::$paramError;
		}
	}
	/**
	 * 已录用列表
	 */
	public function enrollList($data){
		$data['list_rows']=!empty($data['list_rows'])?$data['list_rows']:10;
		$data['page']=!empty($data['page'])?$data['page']:1;
		$page=($data['page']-1)*$data['list_rows'];
		$where=" istd=1 and getid=".$data['user_id'];
		$list=db('deliver_resume')->where($where)->limit($page,$data['list_rows'])->order("end_time desc")->select();
		foreach($list as $k=>$v){
			$info=db('resume')->field('position_id,name,education_id,joinTime,ismy,min,max,user_id,position,birthday,sex')->where('resume_id',$v['resume_id'])->find();
			$userinfo=db('user')->field('headimg,tjid')->where('id',$info['user_id'])->find();
			$info['headimg']=$userinfo['headimg'];
			$info['tjname']=db('user')->where('id',$userinfo['tjid'])->value('name');
			$info['education']=db('education')->where('education_id',$info['education_id'])->value('name');
			$info['positionName']=db('position_cat')->where('id',$info['position_id'])->value('catname');
			$info['gzjy']=$this->countage($info['joinTime']);
			$info['age']=$this->countage($info['birthday']);
			$list[$k]['info']=$info;
			$list[$k]['work']=db('work')->where('id',$v['work_id'])->value('title');
		}
		return $list;
	}
}
